package com.leo;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

/**
 * @ClassName ServiceDemo
 * @Description
 * @Author wangss
 * @date 2020.06.20 22:10
 * @Version 1.0
 */
public class ServiceDemo extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        AnchorPane an = new AnchorPane();
        an.setStyle("-fx-background-color: #f4fffe");

        HBox hBox = new HBox(20);
        hBox.setAlignment(Pos.CENTER);

        Button start = new Button("开始");
        Button cancel = new Button("取消");
        Button restart = new Button("重启");
        Button reset = new Button("重置");

        ProgressBar progressBar = new ProgressBar();
        progressBar.setPrefWidth(200d);
        progressBar.setProgress(0.0);

        Label state = new Label("state");
        Label value = new Label("value");
        Label title = new Label("title");
        Label message = new Label("message");

        hBox.getChildren().addAll(start, cancel, restart, reset, progressBar, state, value, title, message);

        AnchorPane.setTopAnchor(hBox, 100d);
        AnchorPane.setLeftAnchor(hBox, 10d);
        an.getChildren().addAll(hBox);
        Scene scene = new Scene(an);
        primaryStage.setScene(scene);
        primaryStage.setTitle("多任务");
        primaryStage.setWidth(800d);
        primaryStage.setHeight(800d);
        primaryStage.show();

        MyService service = new MyService();

        start.setOnAction(event -> {
            service.start();
        });

        cancel.setOnAction(event -> {
            service.cancel();
        });

        restart.setOnAction(event -> {
            service.restart();
        });

        reset.setOnAction(event -> {
            service.reset();
        });

        service.progressProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                progressBar.setProgress(newValue.doubleValue());
            }
        });

        service.messageProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
                message.setText(newValue);
            }
        });


    }
}
